/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.aseclipse.osgi.loader
{
    import mx.utils.StringUtil;
    
    public class ConfigUtil
    {
        public static const COMMENT_CHAR:String = "#";

        public function ConfigUtil()
        {
        }
        
        /****
        *@param rawData, have multiple lines, every line is a:b/a=b
        *@param operatorSeparator which can be ":" or "="
        */ 
        public static function populateConfigurationData( rawData:String, operatorSeparator:String = "=", fileName : String = null):Object {
            try {
                if ( rawData === null || rawData == "" ) {
                    trace( "Configuration file (" + fileName + ") is empty. Using defaults." );
                    return null;
                }

                var lines:Array = rawData.split( "\n" );
                var configData:Object = new Object( );
                var lastKey:Object = new Object();
                var lineStr:String;
                for ( var line:String in lines ) {
                    lineStr = stripLeadingAndTrailingWhitespace(lines[line]);
                    try {
                        parseLine( configData, lineStr, Number(line) + 1 , operatorSeparator, lastKey);
                    } catch( cex:ConfigParseError ) { 
                        // trace(cex.getStackTrace() );
                        var key:String = lastKey.key as String;
                        configData[key] = configData[key] + lineStr;
                        continue;
                    }
                }
                return configData;
            } catch( ex:Error ) {
                trace(ex.getStackTrace());
            }
            return null;
        }
        
        protected static function parseLine( configData:Object, line:String, lineNumber:Number , operatorSeparator:String, lastKey:Object):void {
            try {
                if ( line == null )
                    return;
                var strippedLine:String = stripLeadingAndTrailingWhitespace( line );
                if ( strippedLine.charAt( 0 ) == COMMENT_CHAR || strippedLine == "" )
                    return;
                return parseConfigValue( configData, strippedLine, lineNumber, operatorSeparator, lastKey);
            } catch( ex:ConfigParseError ) {
                throw ex;
            }
        }
        
        protected static function parseConfigValue( configData:Object, line:String, lineNumber:Number, operatorSeparator:String, lastKey:Object ):void {
            try  {
                var index:Number;
                index = line.indexOf(operatorSeparator);  //  "=" 
                if ( index == -1) {
                    throw new ConfigParseError( "No equal sign found", line, lineNumber );
                }
                var configName:String;
                var configValue:String;
                configName = stripLeadingAndTrailingWhitespace( line.substr( 0, index ) ).toLowerCase();
                configValue = stripLeadingAndTrailingWhitespace( line.substr( index + 1 ) );
                configData[ configName ] = configValue;
                lastKey.key = configName;
            } catch( cex:ConfigParseError ) {
                throw cex;
            }
            // catch( ex:Error ) {
            //    throw new ConfigParseError( "Fail to stripLeadingAndTrailingWhitespace", line, lineNumber );
            // }
        }
        
        protected static function stripLeadingAndTrailingWhitespace( input:String ):String {
            // remove leading whitespace
            //var strippedLine:String = input.replace( /^\s+/g, "" );
            var strippedLine:String = StringUtil.trim(input);
            // remove trailing whitespace
            return strippedLine;//.replace( /\s+$/g, "" );
        }

    }
}